package com.atguigu.gmall.seckill.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/18 20:28 星期二
 * description: 用于超时订单的延迟排队的配置
 */
@Configuration
public class SeckillOrderDelayRabbitConfig {

    /**
     * 延迟队列的正常交换机
     * @return
     */
    @Bean("seckillOrderDelayNormalExchange")
    public Exchange seckillOrderDelayNormalExchange(){
        return ExchangeBuilder.directExchange("seckill_order_delay_normal_exchange").build();
    }

    /**
     * 延迟队列的死信队列
     * @return
     */
    @Bean("seckillOrderDelayDeadQueue")
    public Queue seckillOrderDelayDeadQueue(){
        return QueueBuilder
                .durable("seckill_order_delay_dead_queue")
                .withArgument("x-dead-letter-exchange", "seckill_order_delay_dead_exchange")
                .withArgument("x-dead-letter-routing-key", "seckill.order.normal")
                .build();
    }

    /**
     * 死信队列和正常交换机绑定
     * @param seckillOrderDelayNormalExchange
     * @param seckillOrderDelayDeadQueue
     * @return
     */
    @Bean
    public Binding seckillOrderDelayDeadBinding(@Qualifier("seckillOrderDelayNormalExchange") Exchange seckillOrderDelayNormalExchange,
                                                @Qualifier("seckillOrderDelayDeadQueue") Queue seckillOrderDelayDeadQueue){
        return BindingBuilder
                .bind(seckillOrderDelayDeadQueue)
                .to(seckillOrderDelayNormalExchange)
                .with("seckill.order.dead")
                .noargs();
    }

    /**
     * 延迟队列的死信交换机
     * @return
     */
    @Bean("seckillOrderDelayDeadExchange")
    public Exchange seckillOrderDelayDeadExchange(){
        return ExchangeBuilder.directExchange("seckill_order_delay_dead_exchange").build();
    }

    /**
     * 延迟队列的正常队列
     * @return
     */
    @Bean("seckillOrderDelayNormalQueue")
    public Queue seckillOrderDelayNormalQueue(){
        return QueueBuilder.durable("seckill_order_delay_normal_queue").build();
    }

    /**
     * 死信交换机和正常队列绑定
     * @param seckillOrderDelayDeadExchange
     * @param seckillOrderDelayNormalQueue
     * @return
     */
    @Bean
    public Binding seckillOrderDelayNomalBinding(@Qualifier("seckillOrderDelayDeadExchange") Exchange seckillOrderDelayDeadExchange,
                                                 @Qualifier("seckillOrderDelayNormalQueue") Queue seckillOrderDelayNormalQueue){
        return BindingBuilder
                .bind(seckillOrderDelayNormalQueue)
                .to(seckillOrderDelayDeadExchange)
                .with("seckill.order.normal")
                .noargs();
    }

}
